#!/usr/bin/env bash
# Copyright 2019 Cloudera Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Wrapper around yum install that exits with a non-zero status if a
# package is not found and works around an overlayfs issue

set -eu
set -o pipefail

# This workaround allows us to use yum in a docker host backed by
# overlayfs. It needs to run prior to yum install and in the same
# build layer. Newer (6.8 + and 7.2+) redhat versions provide the
# yum-plugin-ovl package to work around this issue.
# For more information see:
# https://bugzilla.redhat.com/show_bug.cgi?id=1213602
touch /var/lib/rpm/*

t=$(mktemp)
trap "rm -f $t" EXIT
yum -y install "$@" 2>&1 | tee $t
if grep -Eq 'No package .* available.' $t; then
  >&2 echo "yum-install: One or more packages not available but specified as arguments"
  exit 3
fi
